#爬取猿急送兼职任务
import requests
from lxml import etree
import xlwt
from xlwt import Workbook
import re

book = Workbook(encoding='utf-8')
for j in range(1,6):  #获取前5页数据
    if j == 1:
        url = "https://www.yuanjisong.com/job"
    else:
        url = "https://www.yuanjisong.com/job/allcity/page"+str(j)
    sheetname = '第'+str(j)+'页'
    sheetj = book.add_sheet(sheetname)
    # 1. 请求任务网页第一页，抽取第n页接任务网页链接
    response = requests.get(url)
    xml = etree.HTML(response.text) # 整理成xml文档对象
    biaoti_list = xml.xpath('//div[@class="topic_title"]/text()') # 标题
    miaoshu_list = xml.xpath('//div[@class="job_list_item_div"]/div[1]//text()') # 描述
    time_list = xml.xpath('//div[@class="job_list_item_div"]/div[3]//text()')  #工时
    money_list = xml.xpath('//div[@class="job_list_item_div"]/div[4]//text()')  #报酬
    people_list = xml.xpath('//div[@class="weui_panel_bd appoint_div"]//text()')  #当前投递人数
    #print(people_list)
    A = []
    for p in people_list:
        people = re.sub(r'\s','',p+'')
        if people != '':
            A.append(people)
    #print(A)
    B = []
    for i in range(0,len(A)):
        if A[i] == '已完成':
            B.append(A[i])
        elif A[i] == '投递职位':
            if i<len(A)-1 and A[i+1] != '投递职位':
                B.append(A[i+1]+A[i+2]+A[i+3])
            else:
                B.append('')
    #print(B,len(B))
    for i in range(0,len(biaoti_list)):
        miaoshu = miaoshu_list[8*i+3]+miaoshu_list[8*i+4]
        time = time_list[7*i+2]+time_list[7*i+3]+time_list[7*i+4]
        money = money_list[7*i+2]+money_list[7*i+3]+money_list[7*i+4]
        sheetj.write(6*i,0,biaoti_list[i])
        sheetj.write(6*i+1,0,miaoshu)
        sheetj.write(6*i+2,0,time)
        sheetj.write(6*i+3,0,money)
        sheetj.write(6*i+4,0,B[i])

import datetime
now_time = str(datetime.datetime.now())[0:10]
title = now_time+'.xlsx'
book.save(title)

